package cn.itcast.SolrCould;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrServer;

import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

import java.io.IOException;

public class SolrCouldIndextTest {
    @Test
    public void testCreate() throws IOException, SolrServerException {
        CloudSolrServer solrServer=new CloudSolrServer("192.168.206.101:2181,192.168.206.102:2181,192.168.206.103:2181");
        solrServer.setDefaultCollection("myCollection2");

        SolrInputDocument document=new SolrInputDocument();
        document.addField("id","20");
        document.addField("title","iphone7");
        solrServer.add(document);
       solrServer.commit();
    }
    @Test
    public void testDeleteById() throws IOException, SolrServerException {
        CloudSolrServer solrServer=new CloudSolrServer("192.168.206.101:2181,192.168.206.102:2181,192.168.206.103:2181");
        solrServer.setDefaultCollection("myCollection2");

       solrServer.deleteById("20");
       solrServer.commit();
    }
    @Test
    public void testQuery() throws SolrServerException {
        CloudSolrServer solrServer=new CloudSolrServer("192.168.206.101:2181,192.168.206.102:2181,192.168.206.103:2181");
        solrServer.setDefaultCollection("myCollection2");
        QueryResponse response = solrServer.query(new SolrQuery("*:*"));
        SolrDocumentList results = response.getResults();
        for (SolrDocument result : results) {
            String  id = (String) result.getFieldValue("id");
            String id1 = (String) result.get("id1");

            System.out.println(id);
        }
    }
}
